
package com.sfdlJPush.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.http.Header;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;
import com.sfdlJPush.R;

/**
 * @author yushilong
 * @date 2014-10-14 下午6:30:34
 * @version 1.0
 */
public class UpgradeAsyncHttpClient
{
    private AsyncHttpClient mAsyncHttpClient;
    private ProgressBar mProgressBar;
    private TextView mTextView;
    private B5MDialog b5mDialog;
    private Context context;

    public UpgradeAsyncHttpClient(B5MDialog b5mDialog, Context context)
    {
        super();
        this.mAsyncHttpClient = new AsyncHttpClient();
        this.b5mDialog = b5mDialog;
        this.context = context;
        this.mProgressBar = (ProgressBar) b5mDialog.findViewById(R.id.pb);
        this.mTextView = (TextView) b5mDialog.findViewById(R.id.textview);
    }

    public void start(String url)
    {
        mAsyncHttpClient.get(url, new TextHttpResponseHandler()
        {
            @Override
            public void onSuccess(int statusCode , Header[] headers , String responseString)
            {
                // TODO Auto-generated method stub
                System.out.println("responseString=" + responseString);
            }

            @Override
            public void onSuccess(int statusCode , Header[] headers , byte[] responseBytes)
            {
                // TODO Auto-generated method stub
                super.onSuccess(statusCode, headers, responseBytes);
                SDCardUtil sdCardUtil = new SDCardUtil();
                sdCardUtil.createSDDir("sfdlJPush");
                File file = sdCardUtil.createSDFile("sfdlJPush.apk");
                if (saveFileFromByteArray(file, responseBytes))
                {
                    b5mDialog.dismiss();
                    Uri uri = Uri.fromFile(file);
                    Intent installIntent = new Intent(Intent.ACTION_VIEW);
                    installIntent.setDataAndType(uri, "application/vnd.android.package-archive");
                    context.startActivity(installIntent);
                }
            }

            @Override
            public void onFailure(int statusCode , Header[] headers , String responseString , Throwable throwable)
            {
                // TODO Auto-generated method stub
            }

            public void onProgress(int bytesWritten , int totalSize)
            {
                mProgressBar.setMax(totalSize);
                mProgressBar.setProgress(bytesWritten);
                int progress = bytesWritten * 100 / totalSize;
                mTextView.setText(progress + "%");
            };
        });
    }

    public static boolean saveFileFromByteArray(File file , byte[] bytearray)
    {
        try
        {
            if (file.exists())
                file.delete();
            file.createNewFile();
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(file));
            out.write(bytearray);
            out.flush();
            out.close();
            return true;
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return false;
    }

    public void cancel()
    {
        if (mAsyncHttpClient != null)
        {
            mAsyncHttpClient.cancelRequests(context, true);
        }
    }
}
